home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / networking / sockets / example / KnockKnockClient.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  66 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18. import java.net.*;
  19.  
  20. public class KnockKnockClient {
  21.     public static void main(String[] args) {
  22.         Socket kkSocket = null;
  23.         PrintWriter pw = null;
  24.         BufferedReader br = null;
  25.  
  26.         try {
  27.             kkSocket = new Socket("taranis", 4444);
  28.             pw = new PrintWriter(kkSocket.getOutputStream());
  29.             br = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
  30.         } catch (UnknownHostException e) {
  31.             System.err.println("Don't know about host: taranis");
  32.         } catch (IOException e) {
  33.             System.err.println("Couldn't get I/O for the connection to: taranis");
  34.         }
  35.  
  36.         if (kkSocket != null && pw != null && br != null) {
  37.             try {
  38.                 StringBuffer buf = new StringBuffer(50);
  39.                 int c;
  40.                 String fromServer;
  41.  
  42.                 while ((fromServer = br.readLine()) != null) {
  43.                     System.out.println("Server: " + fromServer);
  44.                     if (fromServer.equals("Bye."))
  45.                         break;
  46.                     while ((c = System.in.read()) != '\n') {
  47.                         buf.append((char)c);
  48.                     }
  49.                     System.out.println("Client: " + buf);
  50.                     pw.println(buf.toString());
  51.                     pw.flush();
  52.                     buf.setLength(0);
  53.                 }
  54.  
  55.                 pw.close();
  56.                 br.close();
  57.                 kkSocket.close();
  58.             } catch (UnknownHostException e) {
  59.                 System.err.println("Trying to connect to unknown host: " + e);
  60.             } catch (IOException e) {
  61.                 System.err.println("IOException:  " + e);
  62.             }
  63.         }
  64.     }
  65. }
  66.